<template>
  <el-card class="box-card">
    <template #header>
      <span>患者打卡率</span>
    </template>
    <div ref="chartRef" :style="{ width: width, height: height }"></div>
  </el-card>
</template>
<script lang="ts" setup>
  import { Ref, ref, watch } from 'vue'
  import { useECharts } from '@/hooks/web/useECharts'
  import type { EChartsOption } from 'echarts'

  const animationDuration = 300
  const chartRef = ref<HTMLDivElement | null>(null)
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>, 'macarons')

  const props = defineProps({
    data: {
      type: Object,
      default: {},
    },
    width: {
      // eslint-disable-next-line no-undef
      type: String as PropType<string>,
      default: '100%',
    },
    height: {
      // eslint-disable-next-line no-undef
      type: String as PropType<string>,
      default: '300px',
    },
  })

  watch(
    () => props.data,
    () => {
      if (props.data) {
        const { legendData, seriesData } = props.data
        const seriesDataTmp = {
          tooltip: {
            trigger: 'item',
            backgroundColor: 'rgba(255,255,255,0.8)',
            color: 'red',
            axisPointer: {
              type: 'shadow',
            },
            formatter: (params: any) => {
              return (
                params.name +
                '<br/>' +
                params.marker +
                ' ' +
                params.data.name +
                ' 共' +
                params.data.value +
                '人次'
              )
            },
          },
          grid: {
            top: '0',
          },
          legend: {
            bottom: '10',
            data: legendData,
          },
          series: [
            {
              name: '百分比',
              type: 'pie',
              top: -20,
              label: {
                formatter: '{b} ' + '50%',
              },
              // height: '80%',
              center: ['50%', '50%'],
              radius: ['40%', '70%'],
              data: seriesData,
              animationType: 'scale',
              animationEasing: 'cubicInOut',
              animationDuration,
            },
          ],
        }
        setOptions(seriesDataTmp as EChartsOption)
      }
    },
    { immediate: true }
  )
  // console.log('onBeforeMount1')
  // onMounted(() => {
  //   console.log('onBeforeMount')
  //   setOptions({
  //     tooltip: {
  //       trigger: 'item',
  //     },

  //     series: [
  //       {
  //         name: '访问来源',
  //         type: 'pie',
  //         radius: '80%',
  //         center: ['50%', '50%'],
  //         color: ['#5ab1ef', '#b6a2de', '#67e0e3', '#2ec7c9'],
  //         data: [
  //           { value: 500, name: '电子产品' },
  //           { value: 310, name: '服装' },
  //           { value: 274, name: '化妆品' },
  //           { value: 400, name: '家居' },
  //         ].sort(function (a, b) {
  //           return a.value - b.value
  //         }),
  //         roseType: 'radius',
  //         animationType: 'scale',
  //         animationEasing: 'exponentialInOut',
  //         animationDelay: function () {
  //           return Math.random() * 400
  //         },
  //       },
  //     ],
  //   })
  // })
</script>
<style lang="scss" scoped>
  .box-card {
    // width: 50%;
    margin-bottom: 22px;
  }
</style>
